package com.dupf.product.web.controller.product;

import com.dupf.common.base.BaseController;
import com.dupf.common.util.ItvJsonUtil;
import com.dupf.product.common.constant.ProductResult;
import com.dupf.product.common.constant.ProductResultConstant;
import com.dupf.product.dao.model.ProductSalePriceSetting;
import com.dupf.product.dao.model.ProductSalePriceSettingExample;
import com.dupf.product.rpc.api.ProductSalePriceSettingService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import net.sf.json.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;

/**
 * 产品controller
 * Created by dupf on 2018/3/20.
 */
@Controller
@Api(value = "销售价比例设置", description = "销售价比例设置")
@RequestMapping("/sale/price")
public class ProductSalePriceController extends BaseController {

    private static final Logger LOGGER = LoggerFactory.getLogger(ProductSalePriceController.class);

    @Autowired
    private ProductSalePriceSettingService productSalePriceSettingService;

    @ApiOperation(value = "获取销售价比例设置")
    @RequestMapping(value = "", method = RequestMethod.GET)
    @ResponseBody
    public Object salePrice(HttpServletRequest request) {
        Integer orgId = (Integer) request.getAttribute("currentOrgId");
        Integer userId = (Integer) request.getAttribute("currentUserId");

        Integer requestUserId = (Integer) request.getAttribute("userId");
        if(requestUserId == null){
            requestUserId = userId;
        }

        ProductSalePriceSettingExample productSalePriceSettingExample = new ProductSalePriceSettingExample();
        productSalePriceSettingExample.createCriteria().andOrgIdEqualTo(orgId).andUserIdEqualTo(requestUserId);
        productSalePriceSettingExample.setOrderByClause("id DESC");

        ProductSalePriceSetting productSalePriceSetting = productSalePriceSettingService.selectFirstByExample(productSalePriceSettingExample);

        if (productSalePriceSetting == null) {
            productSalePriceSetting = new ProductSalePriceSetting();
            productSalePriceSetting.setOrgId(orgId);
            productSalePriceSetting.setUserId(requestUserId);
            productSalePriceSetting.setPriceCn(1f);
            productSalePriceSetting.setPriceUs(1f);
            productSalePriceSetting.setPriceCa(1f);
            productSalePriceSetting.setPriceUk(1f);
            productSalePriceSetting.setPriceDe(1f);
            productSalePriceSetting.setPriceEs(1f);
            productSalePriceSetting.setPriceIt(1f);
            productSalePriceSetting.setPriceFr(1f);
            productSalePriceSetting.setPriceMx(1f);
            productSalePriceSetting.setPriceJp(1f);
            productSalePriceSetting.setCtime(System.currentTimeMillis());

            productSalePriceSettingService.insertSelective(productSalePriceSetting);
        }

        return new ProductResult(ProductResultConstant.SUCCESS, productSalePriceSetting);
    }

    @ApiOperation(value = "更新销售价比例设置")
    @RequestMapping(value = "/update", method = RequestMethod.POST)
    @ResponseBody
    public Object update(HttpServletRequest request) throws IOException {
        Integer orgId = (Integer) request.getAttribute("currentOrgId");

        String requestStr = super.getInputStream(request);
        JSONObject data = JSONObject.fromObject(requestStr);

        ProductSalePriceSetting productSalePriceSetting = ItvJsonUtil.readValue(data.toString(), ProductSalePriceSetting.class);

        productSalePriceSetting.setOrgId(orgId);

        int count = productSalePriceSettingService.updateByPrimaryKeySelective(productSalePriceSetting);

        return new ProductResult(ProductResultConstant.SUCCESS, count);
    }

}